home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 April / EnigmA AMIGA RUN 06 (1996)(G.R. Edizioni)(IT)[!][issue 1996-04][Skylink CD V].iso / internet / others / spoolwatch.lha / SpoolWatch / Src / HandleEvents.c < prev    next >
C/C++ Source or Header  |  1995-09-30  |  10KB  |  364 lines

  1.  
  2. /*
  3.  *    Function    HandleEvents
  4.  *    Programmer    N.d'Alterio
  5.  *    Date        11/09/95
  6.  *
  7.  *  Synopsis:    This is the event handler for the GUI of SpoolWatch
  8.  *        it watches for the normal IDCMP messages and also
  9.  *        watches for CTRL-C and update signals. Also changes
  10.  *        window size in line with number of files. Ignores any
  11.  *        files which cannot be placed in a max size window.
  12.  *
  13.  *  Arguments:    struct Window    *win        Pointer to window.
  14.  *        struct IntuiText *it        IntuiText template
  15.  *        struct SWArgs    *swa        Program arguments.
  16.  *        struct FullInfo  *nfi        News FullInfo structure.
  17.  *        struct FullInfo  *mfi        Mail FullInfo structure.
  18.  *
  19.  *  Returns:    0                If successfull.
  20.  *        >0                If error.
  21.  *
  22.  *  Variables:     done                Loop exit flag
  23.  *         exit_code            Function return code.
  24.  *         cur_display            What window is showing flag.
  25.  *         timer_on            Timer success flag.
  26.  *         num_mail            Number of mail in spool.
  27.  *         num_news            Number of news in spool.
  28.  *         max_lines            Max number of msgs.
  29.  *         max_screen_lines        Number of lines that a screen can take.
  30.  *         max_win_height            Height of large window.
  31.  *        *nit                News IntuiText struct.
  32.  *        *mit                Mail IntuiText struct.
  33.  *        *title_str            Small window title.
  34.  *         signals            Returned from Wait().
  35.  *        *msg                Message from IDCMP.
  36.  *        *tr                Time request struct.
  37.  *        *time_msgp            Pointer to timer message port.
  38.  * 
  39.  *  Functions:    UpdateTitleStr            Updates small window title string (SW)
  40.  *        MaxLinesForScreen        Returns how many text lines can fit on screen (SW)
  41.  *        SetWindowTitles            Sets title for window (INT)
  42.  *        CountSpool            Counts message in spool (SW)
  43.  *        BuildIntuiText            Creates IntuiText linked list (SW)
  44.  *        Wait                Waits for a signal (EXEC)
  45.  *        GT_GetIMsg            Gadtools get message (GADTOOLS)
  46.  *        GT_ReplyIMsg            Gadtool reply to a message (GADTOOLS)
  47.  *        PrintIText            Prints text to window. (INT)
  48.  *        ChangeWindowBox            Changes size + position of window (INT)
  49.  *        GT_BeginRefresh            Gadtools refresh window (GADTOOLS)
  50.  *        GT_EndRefresh            Gadtool stop window refresh (GADTOOLS)
  51.  *        FreeIntuiText            Frees IntuiText linked list (SW)
  52.  *        SWError                Display error message (SW)
  53.  *        SendIO                Sends IO request (EXEC)
  54.  *        WaitIO                Waits for IO request to finish (EXEC)
  55.  *        OpenDevice            Opens a device (EXEC)
  56.  *        CloseDevice            Closes a device (EXEC)
  57.  *        CreatePort            Creates a message port (EXEC)
  58.  *        DeletePort            Deletes a message port (EXEC)
  59.  *        CreateExtIO            Creates an IO request (ALIB)
  60.  *        DeleteExtIO            Deletes IO request (ALIB)
  61.  *        
  62.  *  $Id: HandleEvents.c 2.10 1995/09/30 22:02:21 daltern Exp $
  63.  *
  64.  */
  65.  
  66. #include "SpoolWatch.h"
  67.  
  68. #define NEWS 0
  69. #define MAIL 1
  70.  
  71. int HandleEvents( struct Window *win, struct Gadget *gad, struct IntuiText *it, struct SWArgs *swa,
  72.                                       struct FullInfo *nfi, struct FullInfo *mfi )
  73.  
  74. {
  75.  
  76.   int done;
  77.   int exit_code;
  78.   int cur_display;
  79.   int timer_on;
  80.  
  81.   int num_mail;
  82.   int num_news;
  83.   int max_lines;
  84.   int max_screen_lines;
  85.  
  86.   long int max_win_height;
  87.  
  88.   struct IntuiText *nit;
  89.   struct IntuiText *mit;
  90.  
  91.   char title_str[25];
  92.  
  93.   ULONG signals;
  94.  
  95.   struct IntuiMessage *msg;
  96.  
  97.   struct timerequest *tr;
  98.   struct MsgPort     *time_msgp;
  99.  
  100.   timer_on = FALSE;
  101.   if ( time_msgp = (struct MsgPort *)CreatePort( NULL, 0 ) ) {
  102.     if ( tr = (struct timerequest *)CreateExtIO( time_msgp, sizeof( struct timerequest ) ) ) {
  103.         if ( !OpenDevice( TIMERNAME, UNIT_VBLANK, (struct IORequest *)tr, 0 ) ) {
  104.  
  105.             tr->tr_node.io_Command = TR_ADDREQUEST;
  106.               tr->tr_time.tv_secs    = swa->delay;
  107.             tr->tr_time.tv_micro   = 0;
  108.  
  109.             SendIO( (struct IORequest *)tr );
  110.             timer_on = TRUE;
  111.             
  112.         } else {
  113.  
  114.             DeleteExtIO( (struct IORequest *)tr );
  115.             DeletePort( time_msgp );
  116.  
  117.         }   /* end if OpenDev */
  118.     } else {
  119.  
  120.         DeletePort( time_msgp );
  121.  
  122.     }   /* end if CreateExtIo */
  123.   }   /* end if CreatePort */
  124.  
  125.   if ( timer_on ) { 
  126.  
  127. /*
  128.  *   Set title until 1st event occurs.
  129.  */
  130.  
  131.       UpdateTitleStr( title_str, nfi, mfi );
  132.      SetWindowTitles( win, title_str, (UBYTE *) -1 );
  133.   
  134.       num_mail = CountSpool( mfi );
  135.       num_news = CountSpool( nfi );
  136.  
  137.       max_lines = (num_news>num_mail) ? num_news : num_mail;
  138.  
  139.     if ( max_lines > ( max_screen_lines = MaxLinesForScreen( win, gad ) ) ) {
  140.         max_lines = max_screen_lines;;
  141.         SWError( swa, win, "Max window size too small\n Ignoring some articles" );
  142.     }   /* end if max_lines */
  143.  
  144.       nit = BuildIntuiText( it, nfi, max_lines ); 
  145.       mit = BuildIntuiText( it, mfi, max_lines );
  146.  
  147.       if ( nit && mit ) {
  148.  
  149.         max_win_height = win->MaxHeight;
  150.         done = FALSE;
  151.         while ( !done ) {
  152.  
  153.             signals = Wait( (1L<<win->UserPort->mp_SigBit) | (1L<<time_msgp->mp_SigBit) | SIGBREAKF_CTRL_C );
  154.  
  155. /*
  156.  *   Time update signal received.
  157.  */
  158.  
  159.             if ( signals & (1L<<time_msgp->mp_SigBit) ) {
  160.     
  161.                 GetMsg( time_msgp );
  162.  
  163. /*
  164.  *   Free old FullInfo structures and make new updated 
  165.  *   structs. Then if any change update the text structures
  166.  *   and the window size. Number of text lines is limited to
  167.  *   the number of lines that can fit on screen - anything else
  168.  *   is ignored.
  169.  */
  170.  
  171.                 FreeFullInfo( nfi );
  172.                 FreeFullInfo( mfi );
  173.  
  174.                 nfi            = FullSpoolInfo( swa, NEWS_TYPE );
  175.                 mfi            = FullSpoolInfo( swa, MAIL_TYPE );
  176.                 num_mail       = CountSpool( mfi );
  177.                 num_news       = CountSpool( nfi );
  178.                 max_lines  = (num_news>num_mail) ? num_news : num_mail;
  179.  
  180.                 UpdateTitleStr( title_str, nfi, mfi );
  181.  
  182.                 FreeIntuiText( nit );
  183.                 FreeIntuiText( mit );
  184.  
  185.                 if ( max_lines > max_screen_lines ) {
  186.                     max_lines = max_screen_lines;;
  187.                     SWError( swa, win, "Max window size too small\n Ignoring some articles" );
  188.                 }  /* end if max_lines */
  189.  
  190.                 nit = BuildIntuiText( it, nfi, max_lines );
  191.                 mit = BuildIntuiText( it, mfi, max_lines );
  192.  
  193.                 if ( !nit && !mit ) {
  194.                     done      = TRUE;
  195.                     exit_code = FAIL;
  196.                     SWError( swa, win, "Could not allocate IntuiText" );
  197.                 }   /* end if !nit && !mit */
  198.  
  199.                 max_win_height = win->BorderTop + win->BorderBottom + 15 + gad->Height + ((max_lines+2) * it->ITextFont->ta_YSize);
  200.                     
  201.                 if ( win->Width EQ win->MinWidth ) {
  202.                     SetWindowTitles( win, title_str, (UBYTE *)-1 );
  203.                 } else {
  204.                     ChangeWindowBox( win, (long)win->LeftEdge, (long)win->TopEdge, (long)win->MaxWidth, max_win_height );
  205.                 }   /* end if */
  206.  
  207.  
  208. /*
  209.  *   Send new timer request.
  210.  */
  211.  
  212.                 tr->tr_node.io_Command = TR_ADDREQUEST;
  213.                   tr->tr_time.tv_secs    = swa->delay;
  214.                 tr->tr_time.tv_micro   = 0;
  215.  
  216.                 SendIO( (struct IORequest *)tr );
  217.  
  218.             }   /* end if have a timer signal */
  219.  
  220.             if ( signals & SIGBREAKF_CTRL_C ) {
  221.  
  222.                 WaitIO( (struct IORequest *)tr );
  223.                 done      = TRUE;
  224.                 exit_code = SUCCESS;
  225.  
  226.             }   /* end if CTRL-C break */
  227.  
  228.  
  229. /*
  230.  *   IDCMP Events.
  231.  */
  232.  
  233.             if ( signals & (1L<<win->UserPort->mp_SigBit) ) {
  234.     
  235.                 while ( msg = GT_GetIMsg( win->UserPort ) ) {
  236.  
  237.                     switch ( msg->Class ) {
  238.  
  239.                         case IDCMP_CLOSEWINDOW:
  240.     
  241.                             WaitIO( (struct IORequest *)tr );
  242.                             done      = TRUE;
  243.                             exit_code = SUCCESS;
  244.                             break;
  245.  
  246.                         case IDCMP_MOUSEBUTTONS:
  247.  
  248. /*
  249.  *   RMB press - size window if there is anything to display.
  250.  */
  251.  
  252.                             if ( msg->Code EQ MENUUP ) {
  253.                                 if ( max_lines ) {
  254.                                     if ( win->Width EQ win->MinWidth ) {
  255.                                         ChangeWindowBox( win, (long)win->LeftEdge, (long)win->TopEdge, (long)win->MaxWidth, max_win_height );
  256.                                     } else {
  257.                                         ChangeWindowBox( win, (long)win->LeftEdge, (long)win->TopEdge, (long)win->MinWidth, (long)win->MinHeight );
  258.                                     }     /* end if */
  259.                                 }   /* end if */
  260.                             }   /* end if RMB press */
  261.                             break;
  262.     
  263.                         case IDCMP_NEWSIZE:
  264.  
  265. /*
  266.  *   Redraw text once window has resized.
  267.  */
  268.  
  269.                             if ( win->Width EQ win->MaxWidth ) {
  270.     
  271.                                 if ( nfi ) {
  272.                                     SetWindowTitles( win, "News Spool Contents", (UBYTE *)-1 );
  273.                                     PrintIText( win->RPort, nit, (long)(win->BorderLeft+5),(long)(win->BorderTop+15+win->FirstGadget->Height) );
  274.                                     cur_display = NEWS;
  275.                                   } else {
  276.                                     SetWindowTitles( win, "Mail Spool Contents", (UBYTE *)-1 );
  277.                                     PrintIText( win->RPort, mit, (long)(win->BorderLeft+5),(long)(win->BorderTop+15+win->FirstGadget->Height) );
  278.                                     cur_display = MAIL;
  279.                                   }   /* end if */
  280.                             } else {
  281.                                 SetWindowTitles( win, title_str, (UBYTE *) -1 );
  282.                             }   /* end if */
  283.                             break;
  284.  
  285.                         case IDCMP_REFRESHWINDOW:
  286.             
  287.                             GT_BeginRefresh( win );
  288.                             GT_EndRefresh( win, NULL );
  289.                             break;
  290.  
  291.                         case IDCMP_GADGETUP:
  292.  
  293.  
  294. /*
  295.  *   Gadget pressed - toggle news/mail display. If only news or mail
  296.  *   then do nothing.
  297.  */
  298.  
  299.  
  300.                         switch ( cur_display ) {
  301.     
  302.                                 case NEWS:
  303.  
  304.                                     if ( mfi ) {
  305.                                         SetWindowTitles( win, "Mail Spool Contents", (UBYTE *)-1 );
  306.                                         PrintIText( win->RPort, mit, (long)(win->BorderLeft+5),(long)(win->BorderTop+15+win->FirstGadget->Height) );
  307.                                         cur_display = MAIL;
  308.                                     }   /* end if !no_mail */
  309.                                     break;
  310.  
  311.                                 case MAIL:
  312.  
  313.                                     if ( nfi ) {
  314.                                         SetWindowTitles( win, "News Spool Contents", (UBYTE *)-1 );
  315.                                         PrintIText( win->RPort, nit, (long)(win->BorderLeft+5),(long)(win->BorderTop+15+win->FirstGadget->Height) );
  316.                                         cur_display = NEWS;
  317.                                     }   /* end if !no_news */
  318.                         
  319.                             }    /* end switch */
  320.                             break;
  321.  
  322.                     }   /* end switch msg class */
  323.  
  324.                     GT_ReplyIMsg( msg );
  325.  
  326.                 }   /* end while msgs at port */                    
  327.  
  328.             }   /* end if IDCMP message */
  329.  
  330.           }   /* end while messages at port */
  331.  
  332.     } else {
  333.  
  334.         WaitIO( (struct IORequest *)tr );
  335.         SWError( swa, win, "Could not allocate IntuiText" );
  336.         exit_code = FAIL;
  337.  
  338.     }   /* end if mit && nit */
  339.  
  340.     FreeFullInfo( nfi );
  341.     FreeFullInfo( mfi );
  342.  
  343.     FreeIntuiText( mit );
  344.     FreeIntuiText( nit );
  345.  
  346.     CloseDevice( (struct IORequest *)tr );
  347.     DeleteExtIO( (struct IORequest *)tr );
  348.     DeletePort( time_msgp );
  349.  
  350.   } else {
  351.  
  352.     SWError( swa, win, "Could not use time - No Updates" );
  353.     exit_code = FAIL;
  354.  
  355.   }   /* end if timer_on */
  356.  
  357.   return exit_code;
  358.  
  359. }   /* end function HandleEvents */
  360.  
  361. /*========================================================================*
  362.             END FUNCTION HandleEvents
  363.  *========================================================================*/
  364.